
/*========================================================================================
 *                                                                                      *
 *               Copyright(c) 2013-2014, Cha SangWoo, All rights reserved.              *
 *                                                                                      *
 * This file is part of ChaOS.                                                          *
 *                                                                                      *
 * Permission to use, copy, modify, and distribute this software in source and binary   *
 * forms and its documentation for any purpose and without fee is hereby granted,       *
 * provided that the above copyright notice appear in all copies and that both that     *
 * copyright notice and this permission notice appear in supporting documentation.      *
 *                                                                                      *
 * THIS SOFTWARE IS PROVIDED BY THE CHA SANGWOO AND CONTRIBUTORS "AS IS" AND ANY        *
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED            *
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.   *
 * IN NO EVENT SHALL CHA SANGWOO OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,    *
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT         *
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,          *
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF            *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE      *
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,                    *
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                                   *
 *                                                                                      *
========================================================================================*/

#include "co_core.h"
#include "co_log.h"

#if defined (ENABLED_LOG)

#include "stdio.h"
#include "stdlib.h"
#include "stdarg.h"
#include "string.h"

#endif

#ifdef __cplusplus
    extern "C" {
    namespace ChaOS {
#endif

//////////////////////////////////////  < BEGIN >  ///////////////////////////////////////

#if defined (ENABLED_LOG)

CHAR        _log_buffer[LOG_MAX_MSG_LENGTH];

/*======================================================================================*/
/*
    @fn             VOID _co_log_assert(CHAR *pexp, CHAR *pmsg, CHAR *pfunc)

    @brief          

    @param [in]     pexp  - 
    @param [in]     pmsg  - 
    @param [in]     pfunc - 

    @return         None

    - HISTORY -
    [V00.00.01] 2014-03-23 [aireat] - Created.
*/
/*======================================================================================*/
#if (FLAG_USE_ASSERT_MACRO == CO_ENABLE)
VOID _co_log_assert(CHAR *pexp, CHAR *pmsg, CHAR *pfunc)
{
    _co_log_printf("ASSERT", "!!!!! exp[%s] -[%s] at func[%s] !!!!!", pexp, pmsg, pfunc);
    _co_log_printf("ASSERT", "!!!!! exp[%s] -[%s] at func[%s] !!!!!", pexp, pmsg, pfunc);
    _co_log_printf("ASSERT", "!!!!! exp[%s] -[%s] at func[%s] !!!!!", pexp, pmsg, pfunc);

    while(1);
}
#endif

/*======================================================================================*/
/*
    @fn             VOID _co_log_printf(CHAR *pclass, const CHAR *pformat, ...)

    @brief          initialize heap.

    @param [in]     pclass  - 
    @param [in]     pformat - 

    @return         None

    - HISTORY -
    [V00.00.01] 2014-03-23 [aireat] - Created.
*/
/*======================================================================================*/
UINT _co_log_printf(CHAR *pclass, const CHAR *pformat, ...)
{
    va_list     args;
    INT         size, count;
//    CHAR        buffer[LOG_MAX_MSG_LENGTH];

    size = 0;

    // print timestamp
#if (LOG_MSG_ADD_TIMESTAMP)
    {
		size += sprintf(&_log_buffer[size], " %08X ", _g_system_tick); 
    }
#endif

    // add class
#if (LOG_MSG_ADD_CLASS)
    {
        CHAR   *pdest, *psrc;
        int     i;

        psrc  = pclass;
        pdest = &_log_buffer[size];

        for (i = 0; i < LOG_MAX_CLASS_LENGTH; i++)
        {
            if (*psrc)
                *pdest++ = *psrc++;
            else
                *pdest++ = ' ';
        }

        size += LOG_MAX_CLASS_LENGTH;
        _log_buffer[size++] = ' ';
    }
#endif

    // add message
    {
        va_start(args, pformat);

        count = vsnprintf(&_log_buffer[size], (LOG_MAX_MSG_LENGTH-2-size), pformat, args);

        if ((count > 0) && (count < (LOG_MAX_MSG_LENGTH-size-2)))
        {
            size += count;
            _log_buffer[size++] = 0x0A;
            _log_buffer[size++] = 0x0D;
//            size++;
        }
        else
        {
            size = LOG_MAX_MSG_LENGTH;
            _log_buffer[LOG_MAX_MSG_LENGTH-2] = 0x0A;
            _log_buffer[LOG_MAX_MSG_LENGTH-1] = 0x0D;
        }

        va_end(args);
    }

    // print log message
    _port_log_print(_log_buffer, size);

    return size;
}

#endif

//////////////////////////////////////  <  END  >  ///////////////////////////////////////

#ifdef __cplusplus
    } /* namespace ChaOS */
    } /* extern "C" */
#endif

